#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import sys
import pathlib


def parse_file(path):
    ret = {}
    try:
        for line in pathlib.Path(path).read_text().splitlines():
            line = line.strip()
            if line == "" or line.startswith("#"):
                continue
            items = line.split()
            ret[items[0]] = items[1]        # <mirror-name: the-first-mirror-url>
    except FileNotFoundError:
        pass
    return ret


def usage():
    print("Usage:")
    print("  %s <mirror uri>" % (sys.argv[0]))


if __name__ == "__main__":
    if len(sys.argv) == 2 and sys.argv[1] == "--help":
        usage()
        sys.exit(0)

    if len(sys.argv) < 2:
        usage()
        sys.exit(1)

    mirrorUrl = sys.argv[1]
    if not mirrorUrl.startswith("mirror://"):
        print(mirrorUrl)
        sys.exit(0)

    mirrorDict = {}
    mirrorDict.update(parse_file("/var/lib/portage/repo-gentoo/profiles/thirdpartymirrors"))        # FIXME
    mirrorDict.update(parse_file("/etc/portage/mirrors"))

    tstr = mirrorUrl[len("mirror://"):]         # "mirror://github/abc" -> "github/abc"
    tlist = tstr.split("/")                     # "github/abc" -> ["github", "abc"]
    tlist[0] = mirrorDict[tlist[0]]             # ["github", "abc"] -> ["https://github.com", "abc"]
    tstr = "/".join(tlist)                      # ["https://github.com", "abc"] -> "https://github.com/abc"

    print(tstr)
    sys.exit(0)
